#include "ToolMan.h"
#include "DiagramMan.h"
#include "TweakerTool.h"
#include "PrototypeTool.h"
#include "../model/UMLBox.h"
#include "../model/UMLLine.h"

using boost::shared_ptr;

ToolMan::ToolMan(DiagramMan& dman, QWidget* paletteParent)
:	_palette(paletteParent)
{
	addTool(dman, "prototype_tweaker",
			new TweakerTool(dman));

	addTool(dman, "prototype_concrete",
			new PrototypeTool(dman, UMLBox("Concrete")));

	addTool(dman, "prototype_arrowline",
			new PrototypeTool(dman, UMLLine(QPoint(), QPoint(), "nav=>")));
};

QWidget* ToolMan::palette()
{
	return &_palette;
}

void ToolMan::addTool(DiagramMan& dman, const std::string& id, Tool* tool)
{
	shared_ptr<ToolPHB> phb(new ToolPHB(dman, tool));
	tool = NULL; // onwed by PHB now

	_tools.push_back(phb);

	// Note: this get() is OK because Palette guarantees the pointer will not be
	// stored (only connected). Same for the rest of these statements.
	_palette.add(id, phb.get());
}

ToolMan::ToolPHB::ToolPHB(DiagramMan& dman, Tool *tool)
: _dman(dman), _tool(tool)
{
}

void ToolMan::ToolPHB::toggled(bool state)
{
	// Dman gets a reference to us here, let's make sure to fix that before we
	// die.
	if (state)
		_dman.setMouseListener(_tool.get());
}

ToolMan::ToolPHB::~ToolPHB()
{
	// If dman knows about us, make it forget.
	if (_dman.mouseListener() == _tool.get())
		_dman.setMouseListener(NULL);
}

